package com.diaohw.module.file.service.impl;


import com.diaohw.module.file.mapper.AccountFileMapper;
import com.diaohw.module.file.mapper.FileMapper;
import com.diaohw.module.file.obj.entity.AccountFileDO;
import com.diaohw.module.file.obj.entity.FileDO;
import com.diaohw.module.file.service.DownloadService;
import com.diaohw.module.file.utils.MinioUtil;
import com.diaohw.platform.framework.web.util.RequestUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.io.InputStream;

/**
 * <p>
 * 标签 服务类
 * </p>
 *
 * @author D-Hw
 * @since 2024-07-08
 */
@Service
public class DownloadImpl implements DownloadService {
    @Resource
    private FileMapper fileMapper;
    @Resource
    private AccountFileMapper accountFileMapper;

    /**
     * 文件下载
     */
    public void downloadByFileId(String id) {
        FileDO fileInfo = fileMapper.selectOneById(id);
        InputStream download = MinioUtil.download(fileInfo.getBucket(), fileInfo.getPath());

        RequestUtil.download(download, fileInfo.getContentType(), fileInfo.getFilename(), fileInfo.getLength());
    }

    @Override
    public void downloadByAccount(String id) {
        AccountFileDO acc = accountFileMapper.selectOneById(id);
        FileDO file = fileMapper.selectOneById(acc.getFileId());
        InputStream download = MinioUtil.download(file.getBucket(), file.getPath());
        RequestUtil.download(download, file.getContentType(), acc.getFilename(), file.getLength());
    }
}
